/* Department.m
 * A simple enterprise object.
 * 
 * You may freely copy, distribute, and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
 * fitness for any particular use.
 *
 * Written by Mai Nguyen, NeXT Developer Support
 */

#import "Department.h"
#import "strings.h"


@implementation Department

- init 
{
	/* You can add additional intialization here. */
	[super init];
	return self;
}
 
/* DEBUGGING METHODS */
 
- (BOOL)respondsTo:(SEL)sel
{
#ifdef DEBUG 	 
	fprintf(stderr, "RespondsTo: %s\n", sel_getName(sel));
#endif
	return [super respondsTo:sel];
}


/* Accessor methods */

- (void) setDeptId:(NSNumber *)newId
{
	[DeptId release];
	DeptId = [newId retain];
	 
} 

- (void) setDepartmentName:(NSString *)newName
{
	[DepartmentName release];
	DepartmentName = [newName retain];
	 
}

- (void) setLocationId:(NSNumber *)newLocation
{
	[LocationId release];
	LocationId = [newLocation retain];
	 
} 

- (NSArray *) toEmployee
{
	return toEmployee;
}

- (void) setToEmployee:(NSArray *)aRelation
{
	[toEmployee release];
	toEmployee = [aRelation retain];
	 
}
 
- (void) setAverageSalary:(NSNumber *)aSalary
{
	[averageSalary release];
	averageSalary = [aSalary retain];
	 
}

- (NSNumber *)averageSalary
{
	int			i;
	unsigned	count = 0;
	id			anEmployee, value;
	int 		totalSalaries = 0;
	
	if (toEmployee) {
		count = [toEmployee count];
		totalSalaries = 0;
		for (i = 0; i < count; i++) {
			anEmployee = [toEmployee objectAtIndex:i];
			value = [anEmployee objectForKey:@"Salary"];
				/* skip Salary with Null values */
			if  (![value isEqual:[EONull null]])
			totalSalaries += [[anEmployee objectForKey:@"Salary"] intValue];
			} 
	}
	if (count > 0)
 		avgSalaries = totalSalaries/count; 	
	return [NSNumber numberWithInt:avgSalaries];	
		
}


/* This method is just a place holder.  Do not call [super dealloc].
 * Do nothing.
 */
- (void)dealloc
{
   
}

- free
{
   /* Free our ivars, and do all the normal cleanup... */
     [DeptId release];
	 [DepartmentName release];
	 [LocationId release];
	 [FacilityLocation release];
	 [toEmployee release];
	 [averageSalary release];   

    /* Send ourselves a dealloc message so we will be removed
	 *from the uniquing tables.
	 */
    [self dealloc];

    return [super free];
}

@end
